﻿using ReaWTM.LayuiHelper.Extensions;
using System;
using System.Collections.Generic;
using WalkingTec.Mvvm.Core;

//namespace ReaWTM.LayuiHelper.Extensions
namespace WalkingTec.Mvvm.Core
{
	public static class ListVMExtension
	{
		public static GridColumn<T> SetTextBox<T>(this GridColumn<T> self, IBasePagedListVM<T, BaseSearcher> vm, string name = null, string value = null, string emptyText = null, string layVerify = null, string slot = null) where T : TopBasePoco
		{
			if (name == null)
			{
				name = $"{vm.DetailGridPrix}[0].{self.FieldName}";
			}
			self.SetFormat((r, o) => UIServiceExtension.MakeTableCellTextBox(name: name, value: value ?? o?.ToString(), emptyText: emptyText, layVerify: layVerify, slot: slot));
			return self;
		}
		public static GridColumn<T> SetComboBox<T>(this GridColumn<T> self, IBasePagedListVM<T, BaseSearcher> vm, string name = null, List<ComboSelectListItem> value = null, string selectedValue = null, string emptyText = null, string layVerify = null, string slot = null) where T : TopBasePoco
		{
			if (name == null)
			{
				name = $"{vm.DetailGridPrix}[0].{self.FieldName}";
			}
			self.SetFormat((r, o) => UIServiceExtension.MakeTableCellCombo(name: name, value: value, emptyText: emptyText,
				layVerify: layVerify, slot: slot, selectedValue: selectedValue ?? o?.ToString()));
			return self;
		}

		public static GridColumn<T> SetFormat<T>(this GridColumn<T> self, DateTimeTypeEnum dateTimeType) where T : TopBasePoco
		{
			string format = "";
			switch (dateTimeType)
			{
				case DateTimeTypeEnum.DateTime:
					{
						format = "yyyy-MM-dd HH:mm:ss";
						break;
					}
				case DateTimeTypeEnum.Date:
					{
						format = "yyyy-MM-dd";
						break;
					}
				case DateTimeTypeEnum.Month:
					{
						format = "yyyy-MM";
						break;
					}
				case DateTimeTypeEnum.Year:
					{
						format = "yyyy";
						break;
					}
				case DateTimeTypeEnum.Time:
					{
						format = "HH:mm:ss";
						break;
					}
			}

			self.SetFormat(delegate (T r, object o)
			{
				if (o is DateTime dateTime)
				{
					return dateTime.ToString(format);
				}
				else
				{
					return o?.ToString();
				}
			});
			return self;
		}
	}
}
